/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package consumo.face;

import consumo.entity.Usuario;
import consumo.session.UsuarioSession;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;

/**
 *
 * @author Carlos Taschetto
 */
@Named
@SessionScoped
public class LoginFace extends BaseCDIBean {

    private static final long serialVersionUID = 1L;

    @Inject
    private UsuarioSession bean;
    @NotNull
    @Email(message="Informar um login válido!")
    private String login;
    @Size(min=4,message="Informar uma senha com pelo menos 5 caracteres!")
    @NotNull(message="Informar uma senha válida!")
    private String password;
    private boolean userLogged = false;


    public String doLogin(){
        Usuario user = bean.isUsuarioOk(getLogin(), getPassword());
        if(user != null){
            userLogged = true;
        }else {
            userLogged = false;
        }

        if(isUserLogged()){
            if(user.getNome().equals("admin")) return "./administracao/index.faces";
                else return "./inicio/index.faces";
        }else{
            return "index.faces";
        }

    }

    public boolean isUserLogged(){
        return userLogged;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


}
